package com.cx.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cx.entity.Club;
import com.cx.mapper.ClubMapper;
import com.cx.service.IClubService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cx
 * @since 2024-06-07
 */
@Service
public class ClubServiceImpl extends ServiceImpl<ClubMapper, Club> implements IClubService {
    @Override
    public IPage<Club> getClubListPageConditions(Map<String, Object> params) {
        IPage<Club> page = new Page<>(Integer.parseInt((String) params.get("page")), Integer.parseInt((String) params.get("limit")));
        LambdaQueryWrapper<Club> clubLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Map<String, String> paramsMap = null;
        if (params.get("searchParams") != null) {
            String startDate = null,
                    endDate = null;
//          将获取的json格式的字符串转为Java中的对象
            paramsMap = (Map) (JSON.parse((String) params.get("searchParams")));
//          切割日期字符串
            if (!"".equals(paramsMap.get("created"))) {
                startDate = (paramsMap.get("created").split("~"))[0];
                endDate = (paramsMap.get("created").split("~"))[1];
            }

            clubLambdaQueryWrapper.eq(!"".equals(paramsMap.get("id")), Club::getId, paramsMap.get("id"))
                    .like(!"".equals(paramsMap.get("clubName")), Club::getClubName, paramsMap.get("clubName"))
                    .eq(!"".equals(paramsMap.get("level")), Club::getLevel, paramsMap.get("level"))
                    .between(!"".equals(paramsMap.get("created")), Club::getCreated, startDate, endDate)
            ;
        }
        return this.baseMapper.getClubListPageConditions(page, clubLambdaQueryWrapper);
    }
}
